<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>公众号菜单</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-form ref="form" label-width="80px" :inline="true" size="small" class="admin_from">
                <el-form-item>
                    <el-button type="primary" size="small" @click="addRecharge">添加菜单</el-button>
                    <el-button type="success" size="small" @click="publish">发布菜单</el-button>
                </el-form-item>
            </el-form>
            <el-table
                    size="small"
                    :data="tableData"
                    border
                    style="width: 100%">

                <el-table-column
                        type="index"
                        label="#">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="名称">
                    <template slot-scope="scope">
                        <span v-if="scope.row.pid==0">{{scope.row.name}}</span>
                        <span v-else><i class="el-icon-minus"></i>{{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="类型">
                    <template slot-scope="scope">
                            <span v-if="scope.row.type=='view'">跳转URL</span>
                            <span v-if="scope.row.type=='click'">点击推事件</span>
                            <span v-if="scope.row.type=='scancode_push'">扫码推事件</span>
                            <span v-if="scope.row.type=='scancode_waitmsg'">扫码推事件且弹出“消息接收中”提示框</span>
                            <span v-if="scope.row.type=='pic_photo_or_album'">弹出拍照或者相册发图</span>
                            <span v-if="scope.row.type=='pic_sysphoto'">弹出系统拍照发图</span>
                            <span v-if="scope.row.type=='pic_weixin'">弹出微信相册发图器</span>
                            <span v-if="scope.row.type=='location_select'">弹出地理位置选择器</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="extra"
                        label="附加内容">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="150">
                    <template slot-scope="scope">
                        <el-button type="warning" plain size="small" @click="edit(scope.row.id)">修改</el-button>
                        <el-button type="danger" plain size="small" @click="remove(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="el-pagination">
                <el-pagination
                        background
                        @size-change="changeSize"
                        @current-change="changeCurrent"
                        :current-page="params.pagenum"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="params.pagesize"
                        :layout="this.$store.state.paginationLayout"
                        :total="params.totals">
                </el-pagination>
            </div>

        </el-card>
        <!--            新增或编辑-->
        <el-dialog :title="fromTitle" :width="this.$store.state.dialogWidth" :visible.sync="visible">
            <el-row :gutter="20">
                <el-col :span="this.$store.state.elColNum">
                    <el-form ref="form" :label-width="this.$store.state.labelWidth" size="small">
                        <el-form-item label="名称">
                            <el-input v-model="dataForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="排序权值">
                            <el-input v-model="dataForm.sort"></el-input>
                        </el-form-item>
                        <el-form-item label="选择上级菜单"
                                      :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                            <el-select v-model="dataForm.pid" placeholder="选择上级菜单"
                                       :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''" style="width: 100%">
                                <el-option label="顶级菜单" :value="0"></el-option>
                                <el-option :label="item.name" :value="item.id" v-for="(item,index) in allMenu" :key="index"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="附加内容">
                            <el-input v-model="dataForm.extra"></el-input>
                        </el-form-item>
                        <el-form-item label="菜单类型"
                                      :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                            <el-select v-model="dataForm.type" placeholder="选择菜单类型"
                                       :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''" style="width: 100%">
                                <el-option label="点击推事件" value="click"></el-option>
                                <el-option label="跳转URL" value="view"></el-option>
                                <el-option label="扫码推事件" value="scancode_push"></el-option>
                                <el-option label="扫码推事件且弹出“消息接收中”提示框" value="scancode_waitmsg"></el-option>
                                <el-option label="弹出系统拍照发图" value="pic_sysphoto"></el-option>
                                <el-option label="弹出拍照或者相册发图" value="pic_photo_or_album"></el-option>
                                <el-option label="弹出微信相册发图器" value="pic_weixin"></el-option>
                                <el-option label="弹出地理位置选择器" value="location_select"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="dialogAffirm" size="small">确 定</el-button></span>
        </el-dialog>
    </div>
</template>

<script>
  export default {

    name: "User",
    data() {
      return {
        params: {
          pagenum: 1,
          pagesize: 10,
          totals: 0
        },
        dataForm: {
          name: '',
          sort:'',
          pid:'',
          type:'',
          extra:'',
        },
        tableData: [],
        fromTitle: '',
        visible: false,
        id: '',
        allMenu:[],
      }
    },
    created() {
      this.getList();
    },
    methods: {
      changeClick(e) {
        this.$axios.put('admin/user/' + e.id, {
          status: e.status
        }).then((res) => {
          this.$message.success('成功');
          this.getList();
          this.visible = false;
        });
      },
      getList() {
        let data = {
          page: this.params.pagenum,
          page_size: this.params.pagesize,
        };
        this.$axios.get("admin/wechat_menu", data).then((res) => {
          this.params.totals = res.data.count;
          this.tableData = res.data.list;
          this.getAllmenu();
        });
      },
      getAllmenu(){
        let data = {
          page: this.params.pagenum,
          page_size: '99999',
          pid:0
        };
        this.$axios.get("admin/wechat_menu", data).then((res) => {
          this.allMenu = res.data.list;
        });
      },
      // 每页显示多少条
      changeSize(val) {
        this.params.pagesize = val;
        this.getList()
      },
      // 当前显示的页数
      changeCurrent(val) {
        this.params.pagenum = val;
        this.getList()
      },
      // 添加用户
      addRecharge() {
        this.fromTitle = '新增';
        this.id = '';
        this.dataForm = {
          name: '',
          sort:'',
          pid:'',
          type:'',
          extra:'',
        };
        this.visible = true;
      },
      // 编辑用户
      edit(id) {
        this.id = id;
        this.fromTitle = '编辑';
        this.$axios.get('admin/wechat_menu/' + id, {}).then((res) => {
          let data = res.data;
          this.dataForm = data;
          this.visible = true;
        });
      },
      // 确认提交添加或编辑用户
      dialogAffirm() {
        if (this.id) {
          this.$axios.put('admin/wechat_menu/' + this.id, this.dataForm).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        } else {
          // 添加
          this.$axios.post('/admin/wechat_menu', this.dataForm).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        }
      },
      remove(id) {
        this.$confirm('确定要进行操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.del('admin/wechat_menu/' + id, '').then((res) => {
            this.$message.success('删除成功!');
            this.getList();
          });
        }).catch(() => {

        });
      },
      publish(){
        this.$confirm('确定要进行操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post('admin/wechat_menu/push').then((res) => {
            this.$message.success('发布成功!');
            this.getList();
          });
        }).catch(() => {

        });
      }
    }
  }
</script>

<style scoped>
    .img {
        display: flex;
        align-items: center;
    }

    .img > img {
        margin-right: 10px;
    }
</style>